package cn.bs.seckill.controller;

import cn.bs.seckill.pojo.User;
import cn.bs.seckill.service.GoodsService;
import cn.bs.seckill.service.UserService;
import cn.bs.seckill.utils.RedisUtil;
import cn.bs.seckill.vo.DetailVo;
import cn.bs.seckill.vo.GoodsVo;
import cn.bs.seckill.vo.RespBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.concurrent.TimeUnit;


//跳转商品页面
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private UserService userServiceImpl;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;

    //手动渲染前端页面模板
    @RequestMapping(value="/toList",produces = "text/html;charset=UTF-8")
    @ResponseBody
    //通过session获取用户的登录信息
    //通过Model显示用户的信息
    //@CookieValue拿到cookie的值
/*    public String toList(HttpSession session, Model model, @CookieValue("userUUID")String uuid) {
        if (StringUtils.isEmpty(uuid)){
            return "login";
        }
        //通过session拿到用户的登录信息
        User attribute = (User)session.getAttribute(uuid);
  */      //通过redis拿到用户的登录信息
/*        User attribute = userServiceImpl.getUserByCookie(uuid);
        if(null==attribute){
            return "login";
        }
        //让前端显示用户的信息
        model.addAttribute("user",attribute);
        return "goodsList";
    }*/
    public String toList(Model model, User user,HttpServletResponse response,HttpServletRequest request) {
        //通过redis拿到页面,如果不为空返回页面
        String goodsList =(String) redisUtil.get("goodsList");
        if (!StringUtils.isEmpty(goodsList)){
            return goodsList;
        }
        //让前端显示用户的信息
        model.addAttribute("user", user);
        model.addAttribute("goodsList", goodsService.findGoodsVo());
        //如果为空,就要手动渲染,并且存入Redis中
        //拿到tymeleaf模板引擎,这里需要两个参数,一个是要渲染的页面,一个是WebContext环境
        //webcontext需要5个参数req,resp,req的servlet环境和地址,Map类型的model对象(包含数据内容的)
        WebContext webContext = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
        goodsList = thymeleafViewResolver.getTemplateEngine().process("goodsList", webContext);
        if (!StringUtils.isEmpty(goodsList)){
            redisUtil.sSetAndTime("goodList",30, TimeUnit.SECONDS,goodsList);
        }

        return goodsList;

    }

    //商品详情页面
    @GetMapping(value="/detail/{goodsId}" )
    @ResponseBody
    public RespBean toDetail( User user, @PathVariable Long goodsId) {

        //获取商品信息
        GoodsVo goodsVoByGoodsId = goodsService.findGoodsVoByGoodsId(goodsId);
        //得到商品的秒杀开始时间
        Date startDate = goodsVoByGoodsId.getStartDate();
        //得到商品的秒杀结束时间
        Date endDate = goodsVoByGoodsId.getEndDate();
        Date nowDate = new Date();
        //初始化秒杀状态
        int secKillStatus = 0;
        //初始化秒杀倒计时
        int remainSeconds = 0;
        //秒杀还未开始
        if (nowDate.before(startDate)) {
            //秒杀倒计时
            remainSeconds = (int) ((startDate.getTime() - nowDate.getTime()) / 1000);
        } else if (nowDate.after(endDate)) {
            //秒杀结束
            secKillStatus = 2;
            remainSeconds = -1;
        } else {
            //秒杀中
            secKillStatus = 1;
            remainSeconds = 0;
        }
        DetailVo detailVo = new DetailVo();
        detailVo.setUser(user);
        detailVo.setGoodsVo(goodsVoByGoodsId);
        detailVo.setSecKillStatus(secKillStatus);
        detailVo.setRemainSecond(remainSeconds);
        return RespBean.success(detailVo);

    }
//    @RequestMapping(value="/toDetail/{goodsId}",produces ="text/html;charset=UTF-8" )
//    @ResponseBody
//    public String toDetail(Model model, User user, @PathVariable Long goodsId,HttpServletRequest request,HttpServletResponse response) {
//        //通过不同的id从redis拿到页面,如果不为空返回页面
//        String goodsDetail = (String)redisUtil.get("goodsDetail:"+goodsId );
//        if (!StringUtils.isEmpty(goodsDetail)){
//            return goodsDetail;
//        }
//
//        model.addAttribute("user", user);
//        //获取商品信息
//        GoodsVo goodsVoByGoodsId = goodsService.findGoodsVoByGoodsId(goodsId);
//        //得到商品的秒杀开始时间
//        Date startDate = goodsVoByGoodsId.getStartDate();
//        //得到商品的秒杀结束时间
//        Date endDate = goodsVoByGoodsId.getEndDate();
//        Date nowDate = new Date();
//        //初始化秒杀状态
//        int secKillStatus = 0;
//        //初始化秒杀倒计时
//        int remainSeconds = 0;
//        //秒杀还未开始
//        if (nowDate.before(startDate)) {
//            //秒杀倒计时
//            remainSeconds = (int) ((startDate.getTime() - nowDate.getTime()) / 1000);
//        } else if (nowDate.after(endDate)) {
//            //秒杀结束
//            secKillStatus = 2;
//            remainSeconds = -1;
//        } else {
//            //秒杀中
//            secKillStatus = 1;
//            remainSeconds = 0;
//        }
//
//        //返回到前端显示
//        model.addAttribute("remainSeconds",remainSeconds);
//        model.addAttribute("secKillStatus", secKillStatus);
//        model.addAttribute("goods", goodsVoByGoodsId);
//        WebContext webContext = new WebContext(request,response,request.getServletContext(),request.getLocale(),model.asMap());
//        goodsDetail = thymeleafViewResolver.getTemplateEngine().process("goodsDetail", webContext);
//        if (!StringUtils.isEmpty(goodsDetail)){
//            redisUtil.sSetAndTime("goodsDetail",30, TimeUnit.SECONDS,goodsDetail);
//        }
//
//        return goodsDetail;
//    }

}
